package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.thoughtworks.xstream.XStream;

public class ContactDataGenerator {

	public static void main(String[] args) throws IOException {
		if (args.length<2)
		{
			System.out.println("Please, specify 3 parameters: <test data amount>, <file>, <format>");
			return;
		}
		int amount = Integer.parseInt(args[0]);
		File file = new File(args[1]);
		String format = args[2];
		
		if (file.exists())
		{
			System.out.println("File " + file+  " exists, please remove it manually");
			return;
		}
		
		List<ContactData> contacts = generateRandomContacts(amount);
		
		if ("csv".equals(format))
		{
			saveContactsToCsvFile(contacts, file);
		}
		else if ("xml".equals(format))
		{
			saveContactsToXmlFile(contacts, file);
		}
		else 
		{
			System.out.println("Unknown file format "+format);
			return;
		}

	}

	private static void saveContactsToXmlFile(List<ContactData> contacts,
			File file) throws IOException {
		XStream xstream = new XStream();
		xstream.alias("contact", ContactData.class);
		String xml = xstream.toXML(contacts);
		FileWriter writer = new FileWriter(file);
		writer.write(xml);
		writer.close();		
		
	}

	private static void saveContactsToCsvFile(List<ContactData> contacts,
			File file) throws IOException {
		FileWriter writer = new FileWriter(file);
		for (ContactData contact : contacts) {
			writer.write(contact.getFirstName()+","
						+contact.getLastName()+","
						+contact.getAddress()+","
						+contact.getHomePhone()+","
						+contact.getMobilePhone()+","
						+contact.getWorkPhone()+","
						+contact.getEmail()+","
						+contact.getBday()+","
						+contact.getBmonth()+","
						+contact.getByear()+","
						+contact.getSecondaryAddress()+","
						+contact.getSecondaryPhone()+","
						+contact.getId()+",!"+"\n");
		}
		writer.close();
		
	}
	
	public static List<ContactData> loadContactsFromCsvFile(File file) throws IOException {
		List<ContactData> contacts = new ArrayList<ContactData>();
		FileReader reader = new FileReader(file);
		BufferedReader breader = new BufferedReader(reader);
		String line = breader.readLine();
		while (line!=null)
		{
			String[] part=line.split(",");
			ContactData contact = new ContactData()
							.withFirstName(part[0])
							.withLastName(part[1])
							.withAdress(part[2])
							.withHomePhone(part[3])
							.withMobilePhone(part[4])
							.withWorkPhone(part[5])
							.withEmail(part[6])
							.withBday(part[7])
							.withBmonth(Integer.parseInt(part[8]))
							.withByear(part[9])
							.withSecondaryAddress(part[10])
							.withSecondaryPhone(part[11])
							.withContactId(Integer.parseInt(part[12]));
			contacts.add(contact);
			line = breader.readLine();
		}		
		reader.close();
		breader.close();
		return contacts;
	}

	public static List<ContactData> loadContactsFromXmlFile(File file) throws IOException {
		XStream xstream = new XStream();
		xstream.alias("contact", ContactData.class);
		return (List<ContactData>)xstream.fromXML(file);
	}
	
	public static List<ContactData> generateRandomContacts(int amount) {
		List<ContactData> list = new ArrayList<ContactData>();
		for (int i = 0; i < amount; i++) {
			ContactData contact = new ContactData()
				.withFirstName(generateRandomString("first name", 5))
				.withLastName(generateRandomString("last name", 5))
				.withAdress(generateRandomString("adress", 5))
				.withHomePhone(generateRandomInteger(5))
				.withMobilePhone(generateRandomInteger(5))
				.withWorkPhone(generateRandomInteger(5))
				.withEmail(generateRandomEmail(5))
				.withBday(generateRandomInteger(31, 5))
				.withBmonth(generateRandomMonthNumber(5))
				.withByear(generateRandomInteger(1900, 2012, 5))
				.withSecondaryAddress(generateRandomString("secondary address", 5))
				.withSecondaryPhone(generateRandomInteger(5))
				.withContactId(10000);
			list.add(contact);
		}
		return list;
	}
	
	public static String generateRandomString(String text, int period){
		Random rnd = new Random();
		if (rnd.nextInt(period) == 0)
			return "";
		else
			return text + rnd.nextInt();
	}
	
	public static String generateRandomInteger(int threshold, int period){
		Random rnd = new Random();
		if (rnd.nextInt(period) == 0)
			return "-";
		else
			return Integer.toString(rnd.nextInt(threshold-1)+1);
	}
	
	public static int generateRandomMonthNumber(int period){
		Random rnd = new Random();
		if (rnd.nextInt(period) == 0)
			return 0;
		else
			return rnd.nextInt(11)+1;
	}
	
	public static String generateRandomInteger(int min, int max, int period){
		Random rnd = new Random();
		if (rnd.nextInt(period) == 0)
			return "";
		else
			return Integer.toString(min + rnd.nextInt(max-min));
	}
	
	public static String generateRandomInteger(int period){
		Random rnd = new Random();
		if (rnd.nextInt(period) == 0)
			return "";
		else
			return Integer.toString(Math.abs(rnd.nextInt()));
	}
	
	public static String generateRandomEmail(int period){
		Random rnd = new Random();
		//if (rnd.nextInt(period) == 0)
		//	return "";
		return "email" + rnd.nextInt() + "@test.com";
	}

}
